package org.particles.gui;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;

import org.particles.domain.sim.SimulationCase;

/**
 * starts simulation with adjusted parameters.
 */
public class ApplyParamsAction extends AbstractAction {
	private static final long serialVersionUID = 8611457022067616054L;
	private SimCtrlPanel controlPanel;
	private InitSimulationAction initSimAction;
	private WorldPanel worldPanel;
	private WorldTimeTask task;

	public ApplyParamsAction() {
		super();
		initAction();
	}

	/**
	 * must be called after all properties (controlPanel, worldPanel, task) are set.
	 */
	public void initAction() {
		putValue(MNEMONIC_KEY, KeyEvent.VK_A);
		putValue(NAME, "Apply params");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(this.controlPanel.inputErrorsAvailable()) {
			return;
		}
		final boolean running = task.isRunning();
		task.setRunning(false);
		final SimulationCase sim = GuiUtil.applyParametersToWorld(controlPanel);
		if(sim.mustRecreate()) {
			initSimAction.actionPerformed(GuiUtil.emptyActionEvent());
		}
		worldPanel.revalidate();
		task.setRunning(running);
	}

	public void setControlPanel(SimCtrlPanel controlPanel) {
		this.controlPanel = controlPanel;
	}

	public void setInitSimAction(InitSimulationAction initSimAction) {
		this.initSimAction = initSimAction;
	}

	public void setWorldPanel(final WorldPanel worldPanel) {
		this.worldPanel = worldPanel;
	}

	public void setTask(WorldTimeTask task) {
		this.task = task;
	}

}
